package net.comm.encrypt;





import java.text.SimpleDateFormat;

import java.util.*;

import java.lang.Math;


 class temp

{

    static int iLength = 0;

    static String ch = "";

}



public class Decode

{

    public static boolean notConvert;

    public static String strEncode;

    public static int A[] = {31,-42,53,-64,75,-86,97,-108};



    public Decode()

    {

        notConvert = true;

    }



    public static String getStr(String s)

    {

        if(notConvert)

            return s;

        if(s == null)

            return null;

        try

        {

            String s1 = s;

            byte abyte0[] = s1.getBytes("UTF-8");

            String s2 = new String(abyte0);

            return s2;

        }

        catch(Exception exception)

        {

            exception.printStackTrace();

        }

        return "null";

    }



    public static String putStr(String s)

    {

        if(notConvert)

            return s;

        if(s == null)

            return null;

        try

        {

            String s1 = s;

            byte abyte0[] = s1.getBytes("GBK");

            String s2 = new String(abyte0, "UTF-8");

            return s2;

        }

        catch(Exception exception)

        {

            exception.printStackTrace();

        }

        return null;

    }



    public static String dateToString(Date date, String s)

    {

        SimpleDateFormat simpledateformat = new SimpleDateFormat(s);

        return simpledateformat.format(date);

    }



    public static String[] getNowDate()

    {

        String as[] = new String[3];

        GregorianCalendar gregoriancalendar = new GregorianCalendar();

        Date date = gregoriancalendar.getTime();

        as[0] = dateToString(date, "yyyy-MM-dd");

        int l = gregoriancalendar.get(11);

        int i1 = gregoriancalendar.get(12);

        as[1] = "" + (l * 60 + i1);

        gregoriancalendar.add(5, -1);

        as[2] = dateToString(gregoriancalendar.getTime(), "yyyy-MM-dd");

        return as;

    }



    public static String[] mySplit(String s, char c)

    {

        String as[] = null;

        if(s == null)

            return null;

        if(s.length() == 0)

            return null;

        char ac[] = s.toCharArray();

        int l = ac.length;

        int k = 0;

        for(int i = 0; i < l; i++)

            if(ac[i] == c)

                k++;



        as = new String[k + 1];

        int i1 = 0;

        k = 0;

        int j;

        for(j = 0; j < l; j++)

            if(ac[j] == c)

            {

                as[k] = s.substring(i1, j);

                k++;

                i1 = j + 1;

            }



        as[k] = s.substring(i1, j);

        return as;

    }



    public static String toChinese(String s)

    {

        try

        {

            if(s == null)

            {

                return null;

            } else

            {

                s = new String(s.getBytes("UTF-8"), "UTF-8");

                return s;

            }

        }

        catch(Exception exception)

        {

            return null;

        }

    }



    public static String Encode(String s, int i)

    {

        s=s.trim();

        String s1 = "";

        String s2 = "\\x00\\x01\\x02\\x03\\x37\\x2D\\x2E\\x2F\\x16\\x05\\x15\\x0B\\x0C\\x0D\\x0E\\x0F";

        s2 = s2 + "\\x10\\x11\\x12\\x13\\x3C\\x3D\\x32\\x26\\x18\\x19\\x3F\\x27\\x22\\x1D\\x35\\x1F";

        s2 = s2 + "\\x40\\x5A\\x7F\\x7B\\x5B\\x6C\\x50\\x7D\\x4D\\x5D\\x5C\\x4E\\x6B\\x60\\x4B\\x61";

        s2 = s2 + "\\xF0\\xF1\\xF2\\xF3\\xF4\\xF5\\xF6\\xF7\\xF8\\xF9\\x7A\\x5E\\x4C\\x7E\\x6E\\x6F";

        s2 = s2 + "\\x7C\\xC1\\xC2\\xC3\\xC4\\xC5\\xC6\\xC7\\xC8\\xC9\\xD1\\xD2\\xD3\\xD4\\xD5\\xD6";

        s2 = s2 + "\\xD7\\xD8\\xD9\\xE2\\xE3\\xE4\\xE5\\xE6\\xE7\\xE8\\xE9\\xAD\\xE0\\xBD\\x5F\\x6D";

        s2 = s2 + "\\x79\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x91\\x92\\x93\\x94\\x95\\x96";

        s2 = s2 + "\\x97\\x98\\x99\\xA2\\xA3\\xA4\\xA5\\xA6\\xA7\\xA8\\xA9\\xC0\\x4F\\xD0\\xA1\\x07";

        s2 = s2 + "\\x43\\x20\\x21\\x1C\\x23\\xEB\\x24\\x9B\\x71\\x28\\x38\\x49\\x90\\xBA\\xEC\\xDF";

        s2 = s2 + "\\x45\\x29\\x2A\\x9D\\x72\\x2B\\x8A\\x9A\\x67\\x56\\x64\\x4A\\x53\\x68\\x59\\x46";

        s2 = s2 + "\\xEA\\xDA\\x2C\\xDE\\x8B\\x55\\x41\\xFE\\x58\\x51\\x52\\x48\\x69\\xDB\\x8E\\x8D";

        s2 = s2 + "\\x73\\x74\\x75\\xFA\\x25\\xB0\\xB1\\xB3\\xB4\\xB5\\x6A\\xB7\\xB8\\xB9\\xCC\\xBC";

        s2 = s2 + "\\xAB\\x3E\\x3B\\x0A\\xBF\\x8F\\x3A\\x14\\xA0\\x17\\xCB\\xCA\\x1A\\x1B\\x9C\\x04";

        s2 = s2 + "\\x34\\xEF\\x1E\\x06\\x08\\x09\\x77\\x70\\xBE\\xBB\\xAC\\x54\\x63\\x65\\x66\\x62";

        s2 = s2 + "\\x30\\x42\\x47\\x57\\xEE\\x33\\xB6\\xE1\\xCD\\xED\\x36\\x44\\xCE\\xCF\\x31\\xAA";

        s2 = s2 + "\\xFC\\x9E\\xAE\\x8C\\xDD\\xDC\\x39\\xFB\\x80\\xAF\\xFD\\x78\\x76\\xB2\\x9F\\xFF";

        for(int j = 0; j < i; j++)

        {

            char c = s.charAt(j);

            int k = c;

            if(k < 0)

                k += 256;

            s1 = s1 + String.valueOf(s2.charAt(k));

        }



        return s1;

    }



    public static String Decode(String s, int i)

    {

        s=s.trim();

        String s1 = "";

        String s2 = "\\x00\\x01\\x02\\x03\\xCF\\x09\\xD3\\x7F\\xD4\\xD5\\xC3\\x0B\\x0C\\x0D\\x0E\\x0F\\x10\\x11\\x12\\x13\\xC7\\x0A\\x08\\xC9\\x18\\x19\\xCC\\xCD\\x83\\x1D\\xD2\\x1F\\x81\\x82\\x1C\\x84\\x86\\x0A\\x17\\x1B\\x89\\x91\\x92\\x95\\xA2\\x05\\x06\\x07\\xE0\\xEE\\x16\\xE5\\xD0\\x1E\\xEA\\x04\\x8A\\xF6\\xC6\\xC2\\x14\\x15\\xC1\\x1A\\x20\\xA6\\xE1\\x80\\xEB\\x90\\x9F\\xE2\\xAB\\x8B\\x9B\\x2E\\x3C\\x28\\x2B\\x7C\\x26\\xA9\\xAA\\x9C\\xDB\\xA5\\x99\\xE3\\xA8\\x9E\\x21\\x24\\x2A\\x29\\x3B\\x5E\\x2D\\x2F\\xDF\\xDC\\x9A\\xDD\\xDE\\x98\\x9D\\xAC\\xBA\\x2C\\x25\\x5F\\x3E\\x3F\\xD7\\x88\\x94\\xB0\\xB1\\xB2\\xFC\\xD6\\xFB\\x60\\x3A\\x23\\x40\\x27\\x3D\\x22\\xF8\\x61\\x62\\x63\\x64\\x65\\x66\\x67\\x68\\x69\\x96\\xA4\\xF3\\xAF\\xAE\\xC5\\x8C\\x6A\\x6B\\x6C\\x6D\\x6E\\x6F\\x70\\x71\\x72\\x97\\x87\\xCE\\x93\\xF1\\xFE\\xC8\\x7E\\x73\\x74\\x75\\x76\\x77\\x78\\x79\\x7A\\xEF\\xC0\\xDA\\x5B\\xF2\\xF9\\xB5\\xB6\\xFD\\xB7\\xB8\\xB9\\xE6\\xBB\\xBC\\xBD\\x8D\\xD9\\xBF\\x5D\\xD8\\xC4\\x7B\\x41\\x42\\x43\\x44\\x45\\x46\\x47\\x48\\x49\\xCB\\xCA\\xBE\\xE8\\xEC\\xED\\x7D\\x4A\\x4B\\x4C\\x4D\\x4E\\x4F\\x50\\x51\\x52\\xA1\\xAD\\xF5\\xF4\\xA3\\x8F\\x5C\\xE7\\x53\\x54\\x55\\x56\\x57\\x58\\x59\\x5A\\xA0\\x85\\x8E\\xE9\\xE4\\xD1\\x30\\x31\\x32\\x33\\x34\\x35\\x36\\x37\\x38\\x39\\xB3\\xF7\\xF0\\xFA\\xA7\\xyyFF";

        for(int j = 0; j < i; j++)

        {

            char c = s.charAt(j);

            int k = c;

            if(k < 0)

                k += 256;

            s1 = s1 + String.valueOf(s2.charAt(k));

        }



        return s1;

    }



    //判断是否加密后是否落在ASCII为 33-126之间 ，并存入标志为和加密后的一个字符

    //参数一：ch1:加密前的每位字符  iLength:幅值ֵ

    public static boolean IsAsCII(char ch1,int iLength)

    {

        int iLevel = 0;

        int  ch = 0;

        ch = ch1;

        do   //至少执行一次

        {

            ch = ch + iLength;//n*iLength;

            if (ch<0)

                ch+=256;

            if (ch>256)

                ch-=256;

            iLevel++;

        }


        while (ch<33 || ch>126);

        temp.iLength = iLevel;  //存放循环次数

        temp.ch = String.valueOf(ch);   //存放经过转换后的字符

        return true;

    }

    //判断是否解密后是否落在ASCII为 0-256之间,并存入解密后的每一个字符

    //参数一：ch1:加密前的每位字符  iLength:幅值  iFlag:每位循环次数

    public static boolean IsAsCII_DeCode(char ch1,int iLength,char iFlag)

    {

        int  ch = 0;

        ch = ch1;

        /*System.out.println("ch1="+ch1);

        System.out.println("iLength="+iLength);

        System.out.println("iFlag="+iFlag);*/

        do   //至少执行一次

        {

            ch = ch + iLength*Integer.parseInt(String.valueOf(iFlag));

            //System.out.println("ch2="+ch);

            if (ch<0)

                ch+=256;

            if (ch>256)

                ch-=256;

            //System.out.println("ch3="+ch);

        }

        while (ch<0 || ch>256);

        //System.out.println("ch="+ch);

        temp.ch = String.valueOf(ch);

        return true;

    }

    //加密 输入为1-8加密前字符串 ，输出为加密后的串1-8,同时得到标志位

    public static String PwdEncode(String strsrc)

    {

        strsrc=strsrc.trim();

        String temp_src = "";

        String temp_flag = "";

        if (strsrc.equals(""))

           return "";

        for (int i=0;i<strsrc.length();i++)

        {

            if (IsAsCII(strsrc.charAt(i),A[i]))

            {

                temp_src += (String.valueOf((char)Integer.parseInt(temp.ch)));

                temp_flag += String.valueOf(temp.iLength);

            }

        }

        strEncode=temp_flag.trim();

        return temp_src;



    }

    //解码 参数strsrc:1-8位原字符串  strFlag:1-8为标志位

    public static String PwdDecode(String strsrc,String strFlag)

    {

        strsrc=strsrc.trim();

        strFlag=strFlag.trim();

        String strResult = "";

        int tmpvalue=0;

        if (strsrc.equals(""))

            return "";

        if (strsrc.length()!=strFlag.length())

            return "长度不一样，该串不能解码！";

        for (int i=0;i<strsrc.length();i++)

        {

            tmpvalue=strsrc.charAt(i)-A[i]*(Integer.parseInt(String.valueOf(strFlag.charAt(i))));

            if (tmpvalue>0)

            {

              tmpvalue=tmpvalue % 256;

            }

            else if (tmpvalue<0)

            {

              tmpvalue=256-((Math.abs(tmpvalue))%256);	

            }

            strResult+=(String.valueOf((char)tmpvalue));

            /*if (IsAsCII_DeCode(strsrc.charAt(i),-A[i],strFlag.charAt(i)))

                strResult += (String.valueOf((char)Integer.parseInt(temp.ch)));*/

        }

        return strResult;

    }



    /**

     * 解密报文

     * @param acStr 报文密文

     * @return 报文原文

     * @throws Exception

     */

    public static String Decode(String acStr) {

        int i,char_value;

        char ch;

        String str = "";

        char[] ebcdic_to_ascii_table = {0x00,0x01,0x02,0x03,0xCF,0x09,0xD3,0x7F,0xD4,0xD5,0xC3,0x0B,0x0C,0x0D,0x0E,0x0F,

                                        0x10,0x11,0x12,0x13,0xC7,0x0A,0x08,0xC9,0x18,0x19,0xCC,0xCD,0x83,0x1D,0xD2,0x1F,

                                        0x81,0x82,0x1C,0x84,0x86,0xB4,0x17,0x1B,0x89,0x91,0x92,0x95,0xA2,0x05,0x06,0x07,

                                        0xE0,0xEE,0x16,0xE5,0xD0,0x1E,0xEA,0x04,0x8A,0xF6,0xC6,0xC2,0x14,0x15,0xC1,0x1A,

                                        0x20,0xA6,0xE1,0x80,0xEB,0x90,0x9F,0xE2,0xAB,0x8B,0x9B,0x2E,0x3C,0x28,0x2B,0x7C,

                                        0x26,0xA9,0xAA,0x9C,0xDB,0xA5,0x99,0xE3,0xA8,0x9E,0x21,0x24,0x2A,0x29,0x3B,0x5E,

                                        0x2D,0x2F,0xDF,0xDC,0x9A,0xDD,0xDE,0x98,0x9D,0xAC,0xBA,0x2C,0x25,0x5F,0x3E,0x3F,

                                        0xD7,0x88,0x94,0xB0,0xB1,0xB2,0xFC,0xD6,0xFB,0x60,0x3A,0x23,0x40,0x27,0x3D,0x22,

                                        0xF8,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x96,0xA4,0xF3,0xAF,0xAE,0xC5,

                                        0x8C,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,0x97,0x87,0xCE,0x93,0xF1,0xFE,

                                        0xC8,0x7E,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0xEF,0xC0,0xDA,0x5B,0xF2,0xF9,

                                        0xB5,0xB6,0xFD,0xB7,0xB8,0xB9,0xE6,0xBB,0xBC,0xBD,0x8D,0xD9,0xBF,0x5D,0xD8,0xC4,

                                        0x7B,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0xCB,0xCA,0xBE,0xE8,0xEC,0xED,

                                        0x7D,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0xA1,0xAD,0xF5,0xF4,0xA3,0x8F,

                                        0x5C,0xE7,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0xA0,0x85,0x8E,0xE9,0xE4,0xD1,

                                        0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0xB3,0xF7,0xF0,0xFA,0xA7,0xFF};



        for (i=0;i<acStr.length();i++)

        {

            ch = acStr.charAt(i);

            char_value = ch;

            char_value = char_value % 256;

            str += ebcdic_to_ascii_table[char_value];

        }

        return str;

    };



    /**

     * 报文加密

     * @param acStr 报文内容

     * @return 报文原文

     * @throws Exception

     */

    public static String Encode(String acStr) throws Exception

    {

        int i,char_value;

        char ch;

        String str = "";

        char[] ascii_to_ebcdic_table = {0x00,0x01,0x02,0x03,0x37,0x2D,0x2E,0x2F,0x16,0x05,0x15,0x0B,0x0C,0x0D,0x0E,0x0F,  /* 00-0F */

                                        0x10,0x11,0x12,0x13,0x3C,0x3D,0x32,0x26,0x18,0x19,0x3F,0x27,0x22,0x1D,0x35,0x1F,  /* 10-1F */

                                        0x40,0x5A,0x7F,0x7B,0x5B,0x6C,0x50,0x7D,0x4D,0x5D,0x5C,0x4E,0x6B,0x60,0x4B,0x61,  /* 20-2F */

                                        0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0x7A,0x5E,0x4C,0x7E,0x6E,0x6F,  /* 30-3F */

                                        0x7C,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,  /* 40-4F */

                                        0xD7,0xD8,0xD9,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xAD,0xE0,0xBD,0x5F,0x6D,  /* 50-5F */

                                        0x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,  /* 60-6F */

                                        0x97,0x98,0x99,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xC0,0x4F,0xD0,0xA1,0x07,  /* 70-7F */

                                        0x43,0x20,0x21,0x1C,0x23,0xEB,0x24,0x9B,0x71,0x28,0x38,0x49,0x90,0xBA,0xEC,0xDF,  /* 80-8F */

                                        0x45,0x29,0x2A,0x9D,0x72,0x2B,0x8A,0x9A,0x67,0x56,0x64,0x4A,0x53,0x68,0x59,0x46,  /* 90-9F */

                                        0xEA,0xDA,0x2C,0xDE,0x8B,0x55,0x41,0xFE,0x58,0x51,0x52,0x48,0x69,0xDB,0x8E,0x8D,  /* A0-AF */

                                        0x73,0x74,0x75,0xFA,0x25,0xB0,0xB1,0xB3,0xB4,0xB5,0x6A,0xB7,0xB8,0xB9,0xCC,0xBC,  /* B0-BF */

                                        0xAB,0x3E,0x3B,0x0A,0xBF,0x8F,0x3A,0x14,0xA0,0x17,0xCB,0xCA,0x1A,0x1B,0x9C,0x04,  /* C0-CF */

                                        0x34,0xEF,0x1E,0x06,0x08,0x09,0x77,0x70,0xBE,0xBB,0xAC,0x54,0x63,0x65,0x66,0x62,  /* D0-DF */

                                        0x30,0x42,0x47,0x57,0xEE,0x33,0xB6,0xE1,0xCD,0xED,0x36,0x44,0xCE,0xCF,0x31,0xAA,  /* E0-EF */

                                        0xFC,0x9E,0xAE,0x8C,0xDD,0xDC,0x39,0xFB,0x80,0xAF,0xFD,0x78,0x76,0xB2,0x9F,0xFF};    /* F0-FF */



        for (i=0;i<acStr.length();i++)

        {

            ch = acStr.charAt(i);

            char_value = ch;

            char_value = char_value % 256;

            str += ascii_to_ebcdic_table[char_value];

        }

        return str;

    }

}

